package com.kaikeba.servlet.handlermapping.impl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.kaikeba.servlet.handlermapping.HandlerMapping;
import com.kkb.springframework.beans.aware.BeanFactoryAware;
import com.kkb.springframework.beans.config.BeanDefinition;
import com.kkb.springframework.beans.factory.BeanFactory;

public class BeanUrlHandlerMapping implements HandlerMapping,BeanFactoryAware {
	
	private Map<String,Object>mapping=new HashMap<String,Object>();
	private BeanFactory beanFactory;
	
	public void initMethod(){
		Map<String, BeanDefinition> beanDefinitions = beanFactory.getBeanDefinitions();
		for (BeanDefinition bd : beanDefinitions.values()) {
			String name=bd.getBeanName();
			if(name.startsWith("/")){
				Object bean = beanFactory.getBean(name);
				mapping.put(name, bean);
			}
		}
		
	}

	public BeanUrlHandlerMapping() {
		
	}

	@Override
	public Object getHandler(HttpServletRequest request) {
		return mapping.get(request.getRequestURI());
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) {
		this.beanFactory=beanFactory;
	}

}
